package hbase.DemoTest;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.exceptions.DeserializationException;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.List;

public class Scan {
/*    ROW                                                   COLUMN+CELL
 0                                                    column=info:address, timestamp=1721218891643, value=宁波 - 浙江省
 0                                                    column=info:age, timestamp=1721218891643, value=65
 0                                                    column=info:name, timestamp=1721218891643, value=浦炎轩晨
 0                                                    column=info:sex, timestamp=1721218891643, value=女*/

    private static String tableName = "food";
    private static Configuration conf;
    private static Connection HConn;
    private static Admin admin;
    private static Table table;

    public static void main(String[] args) throws IOException, DeserializationException {
        conf = HBaseConfiguration.create();
        HConn = ConnectionFactory.createConnection(conf);
        admin = HConn.getAdmin();
        table = HConn.getTable(TableName.valueOf(tableName));

        org.apache.hadoop.hbase.client.Scan scan = new org.apache.hadoop.hbase.client.Scan();

        // 设置过滤器
        //    查询家庭住在宁波 - 浙江省 的女生  999986
        RowFilter rowFilter = new RowFilter(CompareOperator.EQUAL, new BinaryComparator("999986".getBytes()));
        SingleColumnValueFilter f1 = new SingleColumnValueFilter("info".getBytes(), "addrres".getBytes(), CompareOperator.EQUAL, new BinaryComparator("宁波 - 浙江省".getBytes()));
        SingleColumnValueFilter f2 = new SingleColumnValueFilter("info".getBytes(), "sex".getBytes(), CompareOperator.EQUAL, new BinaryComparator("女".getBytes()));
        FilterList filterList = new FilterList();
        filterList.addFilter(f1);
        filterList.addFilter(f2);
        filterList.addFilter(rowFilter);
        // 添加过滤器组
        scan.setFilter(filterList);

        ResultScanner result = table.getScanner(scan);
        for (Result rs : result) {
            List<Cell> cells = rs.listCells();
            for (Cell cell : cells) {
                // 获取一个单元格可以获取所有内容

                byte[] cloneRow = CellUtil.cloneRow(cell);
                String rowKey = Bytes.toString(cloneRow); // 将字节数据转化为字符窜

                byte[] cloneFamily = CellUtil.cloneFamily(cell);
                String family = Bytes.toString(cloneFamily);

                byte[] cloneQualifier = CellUtil.cloneQualifier(cell);
                String qualifier = Bytes.toString(cloneQualifier);

                byte[] value = CellUtil.cloneValue(cell);
                String values = Bytes.toString(value);

                System.out.println("行间：" + rowKey + " 列柱：" + family + " 列名：" + qualifier + " 值：" + values);
            }
        }
    }
}

